// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// author © fbrit
//@version=5

indicator('Screener MA Cross', overlay=true)

////////////
// INPUTS //

// Moving Averages
len2 = input.int(8, minval=1, title="Length MA8")
len1 = input.int(50, minval=1, title="Length MA50")

// Table Position Options
table_position = input.string(title="Table Position", defval="Top Right", options=["Top Left", "Top Center", "Top Right", "Middle Left", "Middle Center", "Middle Right", "Bottom Left", "Bottom Center", "Bottom Right"])

// Column width and font size
col_width = input.float(5, title="Column Width (%)")
font_size = input.string(title="Font Size", defval="small", options=["tiny", "small", "normal", "large"])

/////////////
// SYMBOLS // 

u01 = input.bool(true, title="", group='Symbols', inline='s01')
u02 = input.bool(true, title="", group='Symbols', inline='s02')
u03 = input.bool(true, title="", group='Symbols', inline='s03')
u04 = input.bool(true, title="", group='Symbols', inline='s04')
u05 = input.bool(true, title="", group='Symbols', inline='s05')
u06 = input.bool(true, title="", group='Symbols', inline='s06')
u07 = input.bool(true, title="", group='Symbols', inline='s07')

s01 = input.symbol('XAUUSD', group='Symbols', inline='s01')
s02 = input.symbol('US30', group='Symbols', inline='s02')
s03 = input.symbol('GBPUSD', group='Symbols', inline='s03')
s04 = input.symbol('EURUSD', group='Symbols', inline='s04')
s05 = input.symbol('USDJPY', group='Symbols', inline='s05')
s06 = input.symbol('USDCAD', group='Symbols', inline='s06')
s07 = input.symbol('GBPJPY', group='Symbols', inline='s07')

//////////////////
// CALCULATIONS //

screener_func(sym) =>
    ma8_15m = request.security(sym, "15", ta.sma(close, len2), lookahead=barmerge.lookahead_off)
    ma50_15m = request.security(sym, "15", ta.sma(close, len1), lookahead=barmerge.lookahead_off)
    ma8_1h = request.security(sym, "60", ta.sma(close, len2), lookahead=barmerge.lookahead_off)
    ma50_1h = request.security(sym, "60", ta.sma(close, len1), lookahead=barmerge.lookahead_off)

    cross_15m = ta.crossover(ma8_15m, ma50_15m)
    crossunder_15m = ta.crossunder(ma8_15m, ma50_15m)
    cross_1h = ta.crossover(ma8_1h, ma50_1h)
    crossunder_1h = ta.crossunder(ma8_1h, ma50_1h)

    ma_cross_status_15m = cross_15m ? "Buy (15m)" : crossunder_15m ? "Sell (15m)" : "No Cross (15m)"
    ma_cross_status_1h = cross_1h ? "Buy (1h)" : crossunder_1h ? "Sell (1h)" : "No Cross (1h)"

    [ma_cross_status_15m, ma_cross_status_1h]

// Security calls to get status for each symbol independently of the chart
[ma_cross01_15m, ma_cross01_1h] = screener_func(s01)
[ma_cross02_15m, ma_cross02_1h] = screener_func(s02)
[ma_cross03_15m, ma_cross03_1h] = screener_func(s03)
[ma_cross04_15m, ma_cross04_1h] = screener_func(s04)
[ma_cross05_15m, ma_cross05_1h] = screener_func(s05)
[ma_cross06_15m, ma_cross06_1h] = screener_func(s06)
[ma_cross07_15m, ma_cross07_1h] = screener_func(s07)

////////////
// ARRAYS //

s_arr = array.new_string(0)
u_arr = array.new_bool(0)
ma_cross_arr_15m = array.new_string(0)
ma_cross_arr_1h = array.new_string(0)

array.push(s_arr, s01)
array.push(s_arr, s02)
array.push(s_arr, s03)
array.push(s_arr, s04)
array.push(s_arr, s05)
array.push(s_arr, s06)
array.push(s_arr, s07)

array.push(u_arr, u01)
array.push(u_arr, u02)
array.push(u_arr, u03)
array.push(u_arr, u04)
array.push(u_arr, u05)
array.push(u_arr, u06)
array.push(u_arr, u07)

array.push(ma_cross_arr_15m, ma_cross01_15m)
array.push(ma_cross_arr_15m, ma_cross02_15m)
array.push(ma_cross_arr_15m, ma_cross03_15m)
array.push(ma_cross_arr_15m, ma_cross04_15m)
array.push(ma_cross_arr_15m, ma_cross05_15m)
array.push(ma_cross_arr_15m, ma_cross06_15m)
array.push(ma_cross_arr_15m, ma_cross07_15m)

array.push(ma_cross_arr_1h, ma_cross01_1h)
array.push(ma_cross_arr_1h, ma_cross02_1h)
array.push(ma_cross_arr_1h, ma_cross03_1h)
array.push(ma_cross_arr_1h, ma_cross04_1h)
array.push(ma_cross_arr_1h, ma_cross05_1h)
array.push(ma_cross_arr_1h, ma_cross06_1h)
array.push(ma_cross_arr_1h, ma_cross07_1h)

///////////
// PLOTS //

// Helper function to set table position based on input
get_table_position(position) =>
    switch position
        "Top Left" => position.top_left
        "Top Center" => position.top_center
        "Top Right" => position.top_right
        "Middle Left" => position.middle_left
        "Middle Center" => position.middle_center
        "Middle Right" => position.middle_right
        "Bottom Left" => position.bottom_left
        "Bottom Center" => position.bottom_center
        => position.bottom_right

// Create table
var tbl = table.new(get_table_position(table_position), 8, 3, frame_color=color.new(#282828, 0), frame_width=1, border_width=2, border_color=color.new(color.blue, 15))

if barstate.islast
    // Enhanced Header Styling
    table.cell(tbl, 0, 0, 'Symbol', width=col_width, text_halign=text.align_center, bgcolor=color.new(color.black, 85), text_color=color.white, text_size=font_size)
    table.cell(tbl, 0, 1, 'MA Cross (15m)', width=col_width, text_halign=text.align_center, bgcolor=color.new(color.black, 85), text_color=color.white, text_size=font_size)
    table.cell(tbl, 0, 2, 'MA Cross (1h)', width=col_width, text_halign=text.align_center, bgcolor=color.new(color.black, 85), text_color=color.white, text_size=font_size)

    // Loop to populate rows with data
    for i = 0 to 6
        if array.get(u_arr, i)
            ma_cross_15m = array.get(ma_cross_arr_15m, i)
            ma_cross_1h = array.get(ma_cross_arr_1h, i)
            color_15m = ma_cross_15m == "Buy (15m)" ? color.green : ma_cross_15m == "Sell (15m)" ? color.red : color.gray
            color_1h = ma_cross_1h == "Buy (1h)" ? color.green : ma_cross_1h == "Sell (1h)" ? color.red : color.gray
    
            table.cell(tbl, i + 1, 0, array.get(s_arr, i), text_halign=text.align_center, bgcolor=color.new(color.gray, 85), text_color=color.white, text_size=font_size)
            table.cell(tbl, i + 1, 1, ma_cross_15m, text_halign=text.align_center, bgcolor=color_15m, text_color=color.white, text_size=font_size)
            table.cell(tbl, i + 1, 2, ma_cross_1h, text_halign=text.align_center, bgcolor=color_1h, text_color=color.white, text_size=font_size)
